package com.linln.admin.SpringBoot.controller;

import com.linln.admin.SpringBoot.domain.Team;
import com.linln.admin.SpringBoot.domain.TeamApply;
import com.linln.admin.SpringBoot.service.TeamApplyService;
import com.linln.admin.SpringBoot.service.TeamService;
import com.linln.admin.SpringBoot.validator.TeamApplyValid;
import com.linln.common.enums.StatusEnum;
import com.linln.common.utils.EntityBeanUtil;
import com.linln.common.utils.ResultVoUtil;
import com.linln.common.utils.StatusUtil;
import com.linln.common.vo.ResultVo;
import com.linln.modules.system.domain.User;
import com.linln.modules.system.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.session.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Airili
 * @date 2020/12/22
 */
@Controller
@RequestMapping("/SpringBoot/teamApply")
public class TeamApplyController{

    @Autowired
    private TeamApplyService teamApplyService;
    @Autowired
    private TeamService teamService;
    @Autowired
    private UserService userService;

    /**
     * 列表页面
     */
    @GetMapping("/index")
    @RequiresPermissions("SpringBoot:teamApply:index")
    public String index(Model model, TeamApply teamApply) {

        // 创建匹配器，进行动态查询匹配
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withMatcher("stu_id", match -> match.contains());
        Session session = SecurityUtils.getSubject().getSession();
        String username = (String) session.getAttribute("user");
        List<TeamApply> content = new ArrayList<>();
        Example<TeamApply> example = Example.of(teamApply, matcher);
        Page<TeamApply> list = teamApplyService.getPageList(example);
        //教师
        if(username.charAt(0)=='2'){

            Long id = userService.getByName(username).getId();
            List<Team> teamList = teamService.getByTeacherId(id);
            for (Team team : teamList) {
                TeamApply byId = teamApplyService.getByTeamId(team.getId());
                if(byId==null){
                    continue;
                }
                content.add(byId);
            }

        }else if (username.charAt(0)=='1'){
            content = teamApplyService.findByStuName(username);

        }else {
            content = list.getContent();
        }


        for (TeamApply teamApply1:content){
            String team_id = teamApply1.getTeam_id();
            System.out.println(team_id+"-----------------");
            Team byId = teamService.getById(Long.parseLong(team_id));
            String team_name = byId.getTeam_name();
            teamApply1.setTeamName(team_name);
            User byName = userService.getByName(teamApply1.getStu_id());
            teamApply1.setStuNickname(byName.getNickname());
        }




        // 封装数据
        model.addAttribute("list", content);
        model.addAttribute("page", list);
        if(username.charAt(0)=='1')
            return "/SpringBoot/teamApply/stu_index";
        return "/SpringBoot/teamApply/index";
    }

    /**
     * 跳转到添加页面
     */
    @GetMapping("/add")
    @RequiresPermissions("SpringBoot:teamApply:add")
    public String toAdd(Model model) {
        Session session = SecurityUtils.getSubject().getSession();
        String username = (String) session.getAttribute("user");
        //Team team = teamService.findByTeam(teamName);
//        List<Team> content = teamService.list();
        // 封装数据
        model.addAttribute("username",username);
        //model.addAttribute("teamId",team.getId());
        return "/SpringBoot/teamApply/add";
    }

    /**
     * 跳转到编辑页面
     */
    @GetMapping("/edit/{id}")
    @RequiresPermissions("SpringBoot:teamApply:edit")
    public String toEdit(@PathVariable("id") TeamApply teamApply, Model model) {
        model.addAttribute("teamApply", teamApply);
        return "/SpringBoot/teamApply/add";
    }

    /**
     * 保存添加/修改的数据
     * @param valid 验证对象
     */
    @PostMapping("/save")
    @RequiresPermissions({"SpringBoot:teamApply:add", "SpringBoot:teamApply:edit"})
    @ResponseBody
    public ResultVo save(@Validated TeamApplyValid valid, TeamApply teamApply) {
        // 复制保留无需修改的数据
        if (teamApply.getId() != null) {
            TeamApply beTeamApply = teamApplyService.getById(teamApply.getId());
            EntityBeanUtil.copyProperties(beTeamApply, teamApply);
        }
        teamApply.setAble(0);
        // 保存数据
        teamApplyService.save(teamApply);
        return ResultVoUtil.SAVE_SUCCESS;
    }

    /**
     * 跳转到详细页面
     */
    @GetMapping("/detail/{id}")
    @RequiresPermissions("SpringBoot:teamApply:detail")
    public String toDetail(@PathVariable("id") TeamApply teamApply, Model model) {
        model.addAttribute("teamApply",teamApply);
        return "/SpringBoot/teamApply/detail";
    }

    /**
     * 设置一条或者多条数据的状态
     */
    @RequestMapping("/status/{param}")
    @RequiresPermissions("SpringBoot:teamApply:status")
    @ResponseBody
    public ResultVo status(
            @PathVariable("param") String param,
            @RequestParam(value = "ids", required = false) List<Long> ids) {
        // 更新状态
        StatusEnum statusEnum = StatusUtil.getStatusEnum(param);
        if (teamApplyService.updateStatus(statusEnum, ids)) {
            return ResultVoUtil.success(statusEnum.getMessage() + "成功");
        } else {
            return ResultVoUtil.error(statusEnum.getMessage() + "失败，请重新操作");
        }
    }
}